import { DateUtil } from "@/utils/DateUtil";
import { RandomUtil } from "@/utils/RandomUtil";
import { noConflict } from "js-cookie";


//服务器返回数据对象
export class DemoExcel {

    public static Status = {
        published: 1, //发布
        draft: 2, //草稿
        delete: 3, //删除
    }

    public static totalNum: number = 100;

    public id: number;
    public time: number;
    public title: string;
    public author: string;
    public importance: number;
    public readings: number;
    public status: number;

    public static modelList: Array<DemoExcel> = []

    public static initTest() {
        this.modelList = [];
        for (let i = 0; i < this.totalNum; i++) {
            let model: DemoExcel = new DemoExcel();
            model.id = i;
            model.time = DateUtil.now() + RandomUtil.nextInt(-100000, 100000);
            model.title = "title" + i;
            model.author = "author" + i;
            model.importance = RandomUtil.nextInt(1, 3);
            model.readings = RandomUtil.nextInt(1, 1000);
            model.status = RandomUtil.nextInt(1, 2)
            this.modelList[i] = model;
        }
    }

    public static getShowList(listQuery: any) {
        this.modelList.sort((a: DemoExcel, b: DemoExcel) => {
            if (listQuery.sort == "+id") {
                return a.id - b.id;
            } else if (listQuery.sort == "-id") {
                return b.id - a.id;
            } else if (listQuery.sort == "+time") {
                return a.time - b.time;
            } else if (listQuery.sort == "-time") {
                return b.time - a.time;
            }
            else {
                return a.id - b.id;
            }
        });

        let showList: Array<DemoExcel> = [];
        let startIndex: number = (listQuery["page"] - 1) * listQuery["limit"]
        let endIndex: number = startIndex + listQuery["limit"]
        for (let i = startIndex; i < endIndex; i++) {
            let model: DemoExcel = this.modelList[i];
            if (model) {
                if (listQuery.title && model.title != listQuery.title) {
                    continue;
                }
                showList.push(model)
            }
        }
        return showList
    }

    public static updateModel(temp: DemoExcel) {
        for (let i = 0; i < this.modelList.length; i++) {
            let model:DemoExcel = this.modelList[i]
            if(model.id == temp.id){
                model.id = temp.id;
                model.author = temp.author;
                model.importance = temp.importance;
                model.readings = temp.readings;
                model.status = temp.status;
                model.time = temp.time;
                model.title = temp.title;
            }
        }
    }
}